package com.bjpowernode.boss.mvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.boss.common.enums.PublicStatus;
import com.bjpowernode.boss.common.enums.ResponseCode;
import com.bjpowernode.boss.common.exception.BossException;
import com.bjpowernode.boss.common.utils.NumberUtil;
import com.bjpowernode.boss.dao.entity.PayProduct;
import com.bjpowernode.boss.dao.mapper.PayProductMapper;
import com.bjpowernode.boss.dao.result.DicEntity;
import com.bjpowernode.boss.mvc.models.form.payproduct.PayProductForm;
import com.bjpowernode.boss.mvc.service.PayProductService;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author yu
 * @date 2024/7/11  18:08
 */

@Service
public class PayProductServiceImpl implements PayProductService {

    @Resource
    private PayProductMapper payProductMapper ;

    @Override
    public IPage<PayProduct> pageList(Integer pageNo, Integer pageSize) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>() ;
        wrapper.orderByDesc("edit_time") ;

        IPage<PayProduct> payProductIPage = payProductMapper.selectPage(Page.of(pageNo, pageSize), wrapper) ;

        return payProductIPage ;
    }

    @Override
    public int addPayProduct(PayProductForm payProductForm) {
        // 产品编号 和 产品名称 都是唯一的
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>() ;
        wrapper.eq("product_code", payProductForm.getProductCode().trim()) ; // 去除空格

        PayProduct payProduct = payProductMapper.selectOne(wrapper) ;

        if (payProduct != null) {
            throw new BossException(ResponseCode.PAYMENT_PRODUCT_CODE_EXISTS) ;
        }

        wrapper.clear();
        wrapper.eq("product_name", payProductForm.getProductName()) ;
        payProduct = null ;
        payProduct = payProductMapper.selectOne(wrapper) ;
        if (payProduct != null) {
            throw new BossException(ResponseCode.PAYMENT_PRODUCT_NAME_EXISTS) ;
        }

        payProduct = new PayProduct() ;
        payProduct.setId(NumberUtil.getUUID());
        payProduct.setProductCode(payProductForm.getProductCode());
        payProduct.setProductName(payProductForm.getProductName());
        payProduct.setStatus(PublicStatus.DISABLE.name());
        payProduct.setEditTime(new Date());
        payProduct.setRemark("新建支付产品");

        return payProductMapper.insert(payProduct) ;
    }

    @Override
    public List<DicEntity> queryProductDicList() {
        List<DicEntity> dicEntityList = payProductMapper.selectProductDicList() ;
        return dicEntityList ;
    }
}
